package cn.eyesea.cc.frame.component;

import cn.eyesea.cc.config.GlobalConfig;
import cn.eyesea.cc.entity.FieldInfo;
import cn.eyesea.cc.frame.base.ResourceFunction;
import cn.eyesea.cc.frame.base.ResultResource;
import cn.eyesea.cc.frame.base.impl.resultresource.JavaClassResultResource;
import cn.eyesea.cc.frame.base.impl.resultresource.VariableResultResource;
import cn.eyesea.cc.frame.enums.LogicalTypeEnum;
import cn.eyesea.cc.frame.enums.ResultTypeEnum;
import cn.eyesea.cc.util.NameUtils;

/**
 * TODO
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2021/8/17 10:18 下午
 */
public class ResourceComponent {


    public static VariableResultResource convert(FieldInfo fieldInfo) {
        VariableResultResource variableResultResource = new VariableResultResource();
        variableResultResource.setName(NameUtils.getInstance().getJavaName(fieldInfo.getField()));
        ResultResource resultResource = new ResultResource() {
            @Override
            public void usage(ResourceFunction resourceFunction) {
            }

            @Override
            public void usage(ResultResource resultResource) {
            }

            @Override
            public void generateCode(GlobalConfig globalConfig) {
            }
        };
        resultResource.setNeedGenerateCode(false);
        variableResultResource.setVariableType(resultResource);
        variableResultResource.setComment(fieldInfo.getName());
        String javaType = fieldInfo.getJavaType();
        resultResource.setPackageInfo(javaType.substring(0, javaType.lastIndexOf(".")));
        resultResource.setName(javaType.substring(javaType.lastIndexOf(".") + 1));
        return variableResultResource;
    }

    public static String getResourceFullName(ResultResource resultResource) {
        String fullName = null;
        ResultTypeEnum resourceType = resultResource.getResourceType();
        switch (resourceType) {
            case XML:
                break;
            case JAVA_CLASS:
                JavaClassResultResource javaClassResultResource = (JavaClassResultResource) resultResource;
                fullName = NameComponent.getFullName(javaClassResultResource);
                break;
            case JAVA_INTERFACE:
                break;
            case JAVA_VARIABLE:
                VariableResultResource variableResultResource = (VariableResultResource) resultResource;
                fullName = NameComponent.getFullName(variableResultResource.getVariableType());
                break;
            default:
                throw new RuntimeException("resourceType不合法");
        }
        return fullName;
    }
}
